package com.yaodian100.mall.member.web;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.member.entity.Balance;
import com.yaodian100.ec.member.entity.Bonus;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.payment.entity.BonusTransfer;
import com.yaodian100.ec.payment.service.TransferService;

/**
 * 
 * @author apple.chang
 * 
 */
public class MyBonusAction extends ActionSupport {

	private static final long serialVersionUID = 8869900313030803419L;

	private MemberService memberService;
	private TransferService transferService;
	private Member member;
	private List<Bonus> bonusList;
	private List<BonusTransfer> bonustransferList;
	private BigDecimal canUse;
	private String waitUse;
	private String nearlyD;
	private String nearlyP;

	/**
	 * 我的購物金
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String tomybonus() throws CoreException {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		member = memberService.getMemberByLoginId(UserLoginData.getLoginId(), request.getServerName());

		Balance balance = member.getBalance();
		canUse = balance.getBonusBalance();
		waitUse = memberService.getWaitStartBonus(member.getMemberId());

		// 即將過期點數
		String nearlyBonus = memberService.getLastExpireBonus(member.getMemberId());
		if (StringUtils.isBlank(nearlyBonus)) {
			nearlyD = "";
			nearlyP = "";
		} else {
			String[] tmp = nearlyBonus.split("@");
			nearlyD = tmp[0];
			nearlyP = tmp[1];
		}
		return "tomybonus";
	}

	/**
	 * 累點明細
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String accumulatebonus() throws CoreException {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		member = memberService.getMemberByLoginId(UserLoginData.getLoginId(), request.getServerName());

		CommonCriteria crit = new CommonCriteria();
		crit = new CommonCriteria();
		crit.addEq("memberId", member.getMemberId());
		crit.addGe("expireDate", DateUtils.addMonths(new Date(), -6));
		bonusList = memberService.getBonusList(crit, new String[] { "createDate" });

		return "accumulatebonus";
	}

	/**
	 * 使用明細
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String tousebonus() throws CoreException {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		member = memberService.getMemberByLoginId(UserLoginData.getLoginId(), request.getServerName());

		// 使用明細
		CommonCriteria crit = new CommonCriteria();
		crit = new CommonCriteria();
		crit.addEq("memberId", member.getMemberId());
		crit.addGe("createDate", DateUtils.addMonths(new Date(), -6));
		bonustransferList = transferService.getBonusTransferList(0, -1, crit, new String[] { "createDate" });

		return "tousebonus";
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public List<Bonus> getBonusList() {
		return bonusList;
	}

	public void setBonusList(List<Bonus> bonusList) {
		this.bonusList = bonusList;
	}

	public BigDecimal getCanUse() {
		return canUse;
	}

	public void setCanUse(BigDecimal canUse) {
		this.canUse = canUse;
	}

	public String getNearlyD() {
		return nearlyD;
	}

	public void setNearlyD(String nearlyD) {
		this.nearlyD = nearlyD;
	}

	public String getNearlyP() {
		return nearlyP;
	}

	public void setNearlyP(String nearlyP) {
		this.nearlyP = nearlyP;
	}

	public void setTransferService(TransferService transferService) {
		this.transferService = transferService;
	}

	public List<BonusTransfer> getBonustransferList() {
		return bonustransferList;
	}

	public void setBonustransferList(List<BonusTransfer> bonustransferList) {
		this.bonustransferList = bonustransferList;
	}

	public String getWaitUse() {
		return waitUse;
	}

	public void setWaitUse(String waitUse) {
		this.waitUse = waitUse;
	}

}